package com.hwh.trafficdemo.utils.BusUtils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hwh.trafficdemo.dao.IRouteDao;
import com.hwh.trafficdemo.dao.IStopDao;
import com.hwh.trafficdemo.entity.*;
import com.hwh.trafficdemo.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * 用于获取实时公交数据
 */

@Component
public class BusData {

    private static ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    @Autowired
    IRouteDao routeDao;

    @Autowired
    IStopDao stopDao;

    @Transactional
    public void insertRouteInfo(String busName) throws IOException {
        try {
            String url_1 = "https://app.ibuscloud.com/v11/bus/findRouteByName?" + new BusApiParam_1(busName).toString();
            //获取route_json字符串
            String route_json = HttpUtils.httpGet(url_1);

            BusDomJson busDomJson = MAPPER.readValue(route_json, BusDomJson.class);
            List<Items> list = busDomJson.getItems();
            if (list.size() != 0) {
                Items items = list.get(0);
                Routes routes = items.getRoutes().get(0);
                routeDao.savaRoute(routes);
            }
        } catch (Exception e) {
        }

    }

    @Transactional
    public void insetStopInfo(String busName) throws IOException {
        try {
        String url_1 = "https://app.ibuscloud.com/v11/bus/findRouteByName?" + new BusApiParam_1(busName).toString();
        //获取route_json字符串
        String route_json = HttpUtils.httpGet(url_1);
        BusDomJson busDomJson = MAPPER.readValue(route_json, BusDomJson.class);
        List<Items> list = busDomJson.getItems();
        if (list.size() != 0) {
            Items items = list.get(0);
            long routeId = items.getRoutes().get(0).getRouteId();
            String url_2 = "https://app.ibuscloud.com/v11/bus/getNextBusByRouteStopId?" + new BusApiParam_2(String.valueOf(routeId)).toString();
            String realtime_bus_data_json = HttpUtils.httpGet(url_2);
            BusDomJson busDomJson2 = MAPPER.readValue(realtime_bus_data_json, BusDomJson.class);
            List<Stops> stops = busDomJson2.getItem().getStops();
            if (stops.size() != 0) {
                for (Stops stop : stops) {
                    try {
                        stopDao.saveStop(stop);
                    } catch (DuplicateKeyException e) {
                        System.out.println("出错");
                    }
                }

            }
        }


        }catch (Exception e){

        }


    }


    public BusDomJson getAllBusData(String busRouteName, String stopName) {

        String busApi = getBusApi(busRouteName, stopName);

       /* String url_1 = "https://app.ibuscloud.com/v11/bus/findRouteByName?"+new BusApiParam_1(busRouteName).toString();
        //获取route_json字符串
        String route_json = HttpUtils.httpGet(url_1);
        //解析JSON字符串获取routeid用来进行实时公交数据的获取
        String routeId = getRouteId(route_json);



        String url_2 = "https://app.ibuscloud.com/v11/bus/getNextBusByRouteStopId?"+new BusApiParam_2(routeId).toString();*/
        String realtime_bus_data_json = HttpUtils.httpGet(busApi);
        BusDomJson busDomJson = null;
        try {
            busDomJson = MAPPER.readValue(realtime_bus_data_json, BusDomJson.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return busDomJson;
    }


    public String getRouteId(String route_json) throws IOException {

        //解析JSON数据获取RouteId
        BusDomJson busDomJson = MAPPER.readValue(route_json, BusDomJson.class);
        List<Items> list = busDomJson.getItems();
        //获取routeId
        Items items = list.get(0);
        long routeId = items.getRoutes().get(0).getRouteId();
        //long oppositeId = items.getRoutes().get(0).getOppositeId();
        //System.out.println(routeId);
        return String.valueOf(routeId);
    }


    public String getBusApi(String routeName, String stopName) {
        Long routeId = routeDao.findRouteIdByName(routeName);
        List<Long> stopIds = stopDao.findStopIdByName(stopName);
        Long stopId = stopIds.get(0);
        String busApi = "https://app.ibuscloud.com/v11/bus/getNextBusByRouteStopId?" + new BusApiParam_2(String.valueOf(routeId), String.valueOf(stopId)).toString();
        return busApi;
    }
}




/*class tt{
    public static void main(String[] args) throws IOException {
        BusData busData = new BusData();
        //busData.getRouteId("");

        while (true) {
            BusDomJson allBusData = busData.getAllBusData("197");
            Item item = allBusData.getItem();
            List<Stops> stops = item.getStops();
            NextBuses nextBuses = item.getNextBuses();

            String stopName = nextBuses.getStopName();//当前站点名称
            System.out.println("当前站点名称为:"+stopName);
            int n = 1;
            for (Stops stop : stops) {
                List<Buses> buses = stop.getBuses();
                if (buses.size() != 0) {
                    //遍历BUS的信息
                    Buses bus = buses.get(0);
                    System.out.println("第"+ n++ +"辆公交在" + stop.getStopName());
                    System.out.println("距离下一站:"+bus.getNextDistance()+"米");//距离下一站
                    int d = bus.getNextDistance();
                    int s = bus.getTargetSeconds();
                    double v =  new BigDecimal((float)d/s).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    System.out.println("实时车速为:"+v+"m/s");//行驶到下一站需要几秒
                    System.out.println("---------------");
                }

            }
            //每隔5秒获取一次实时公交信息
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}*/

